package com.prone.vyuan.widget.dpdialog;

import com.dongdongsongcan.app.R;
import com.su.dolphin.utils.SystemUtil;
import com.su.dolphin.utils.ViewUtil;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;

public class DialogTool {
	private static final String TAG = "DialogTool";

	/**
	 * 构建一个AlertDialog
	 * 
	 * @param ctx
	 *            上下文
	 * @param icon
	 *            标题栏菜单
	 * @param title
	 *            标题栏文字
	 * @param contentView
	 *            按钮内容区域布局
	 * @param positiveBtnText
	 *            确认按钮文字
	 * @param positiveOnClickListener
	 *            确认按钮单击事件
	 * @param negativeBtnText
	 *            取消按钮文字
	 * @param negativeOnClickListener
	 *            取消按钮单击事件
	 * @return 构建好的Dialog
	 */
	public static CustomDialog buildCustomDialog(Context ctx, int icon,
			String title, View contentView, String positiveBtnText,
			DialogInterface.OnClickListener positiveOnClickListener,
			String negativeBtnText,
			DialogInterface.OnClickListener negativeOnClickListener) {
		CustomDialog.Builder dialogBuilder = new CustomDialog.Builder(ctx);
		dialogBuilder.setTitle(title);
		dialogBuilder.setContentView(contentView);

		dialogBuilder.setTitleIcon(icon);
		if (!TextUtils.isEmpty(positiveBtnText)) {
			dialogBuilder.setPositiveButton(positiveBtnText,
					positiveOnClickListener);
		}
		if (!TextUtils.isEmpty(negativeBtnText)) {
			dialogBuilder.setNegativeButton(negativeBtnText,
					negativeOnClickListener);
		}
		return dialogBuilder.create();
	}

	/**
	 * 显示一个最简单的dialog, 他显示的内容在外面填充
	 * 
	 * @param context
	 * @param contentView
	 * @return
	 */
	public static Dialog buildPlainDialog(Context context, View contentView) {

		// 设置位置
		// WindowManager.LayoutParams windowParams = new
		// WindowManager.LayoutParams();
		// windowParams.y = SystemUtil.getScreenWH()[1];
		// Window window = dialog.getWindow();
		// window.setAttributes(windowParams);

		Dialog dialog = new Dialog(context, R.style.Dialog);
		dialog.setCanceledOnTouchOutside(true);
		dialog.setContentView(contentView, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		dialog.getWindow().setLayout(
				SystemUtil.getScreenWH()[0] - ViewUtil.dip2px(30),
				LayoutParams.WRAP_CONTENT);
		return dialog;
	}
}
